/**
 * Created by Maibenben on 2017/5/4.
 */
$(function () {
    //跳转到本页之后读取cookie中是否有mid和token
    checkCookie();
    var mid = $.cookie("mid");
    var token = $.cookie("token");
    var important = "&mid=" + mid + "&token=" + token;
    var databack = false;
    $.get(payInfoURL + location.search + important, function (res) {
        $(".load").hide();
        databack = true;
        if (res.is_card_pay == 0) {
            $(".card").hide();
            $(".right>li").eq(2).remove();
        }
        if (res.remain <= 0) {
            $(".center").text("该订单已经过期");
            $(".footer").remove();
        }
        $(".price").text(res.register_price);
        //倒计时显示
        var last = res.remain;
        var timer = setInterval(function () {
            last -= 0.1;
            var m = Math.floor(last / 60);
            var s = Math.floor(last - m * 60);
            s = s > 9 ? s : "0" + s;
            $(".last-time").text(m + ":" + s);
            if (last <= 0) {
                clearInterval(timer);
                $(".center").text("该订单已经过期");
                $(".footer").off("click");
            }
        }, 100);
        $(".balance>i").text(res.money + "元");
        if (parseFloat(res.money) > parseFloat(res.register_price)) {
            $(".red").hide();
        }
    });
    //选择支付方式
    //console.log($(".right").find("img").attr("src"));
    var pindex;
    $('.right img').click(function () {
        $('.right img').attr('src', '../imgs/no_check.png');
        $(this).attr('src', '../imgs/check.png');
        pindex = $(this).parent().index();
    });
    $(".footer").on("click", function () {
        if (databack) {
            ////判断是哪一种支付方式
            //if ($(".right>li").length == 3) {
            //    switch (pindex) {
            //        case 0:
            //            console.log("就诊卡支付");
            //            break;
            //        case 1:
            //            // console.log("微信支付");
            //
            //            break;
            //        case 2:
            //            console.log("银联支付");
            //            break;
            //        default :
            //            console.log("就诊卡支付");
            //            break;
            //    }
            //} else {
            //    switch (pindex) {
            //        case 0:
            //            wxpay();
            //            break;
            //        case 1:
            //            console.log("银联支付");
            //            break;
            //        default :
            //            wxpay();
            //            break;
            //    }
            //}
            $(this).off("click");
            wxpay();
        }
        //self.location.href = "../html/purchase-success.html";
    });
});

var config = '';

function onBridgeReady() {
    if (typeof WeixinJSBridge === 'undefined') {
        alert('请在微信页打开页面！');
        return false;
    }
    WeixinJSBridge.invoke(
        'getBrandWCPayRequest',
        {
            "appId": config.appId,     //公众号名称，由商户传入
            "timeStamp": config.timeStamp,         //时间戳，自1970年以来的秒数
            "nonceStr": config.nonceStr, //随机串
            "package": config.package,
            "signType": "MD5",         //微信签名方式：
            "paySign": config.paySign //微信签名
        },
        function (res) {
            switch (res.err_msg) {
                case 'get_brand_wcpay_request:cancel':
                    alert('用户取消支付！');
                    history.go(-1);
                    break;
                case 'get_brand_wcpay_request:fail':
                    alert('支付失败！（' + res.err_desc + '）');
                    break;
                case 'get_brand_wcpay_request:ok':
                    //跳转成功页
                    window.location.href = 'pay_success.html';
                    break;
                default:
                    alert(JSON.stringify(res));
                    break;
            }
        });

}


function Request(name) {
    new RegExp("(^|&)" + name + "=([^&]*)").exec(window.location.search.substr(1));
    return RegExp.$2
}

function wxpay() {
    getData({
        url: apiwxpay,
        data: {mid: $.cookie('mid'), token: $.cookie('token'), aid: Request('aid')}
    }, function (data) {
        if (data.state) {
            config = data.data.payconfig;
            if (typeof WeixinJSBridge == "undefined") {
                if (document.addEventListener) {
                    document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
                } else if (document.attachEvent) {
                    document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                    document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
                }
            } else {
                onBridgeReady();
            }
        } else {

            alert(data.msg);
        }

    })
}